package com.concerneyes.task;

import android.os.Build;
import android.text.TextUtils;

import com.concerneyes.CEApp;

import java.util.HashMap;
import java.util.Map;

import common.PreferUtil;
import http.HttpAsyncTask;
import http.Url;

/**
 * 用户设备信息统计以及推送绑定
 */
public class DeviceInfo {
    String userId = PreferUtil.getUserId();

    public DeviceInfo() {
        if (userId != null) {
            setDeviceInfo();
            String registrationID = PreferUtil.getRegistrationID();
            if (registrationID != null) {
                bindPush(registrationID);
            }
        }
    }

    /**
     * 用户设备信息统计
     */
    private void setDeviceInfo() {
        String model = Build.MODEL;
        //手机品牌
        String brand = Build.BRAND;
        //设备类型
        String device = "android";
        //操作系统版本
        String osVersion = Build.VERSION.RELEASE;
        //软件版本
        String appVersion = CEApp.getVersion();
        //向服务器发送设备的基本信息
        String url = Url.BaseUrl + Url.UserLog;
        Map<String, String> params = new HashMap<>();
        params.put("uid", userId);
        params.put("model", model);
        params.put("brand", brand);
        params.put("device", device);
        params.put("osVersion", osVersion);
        params.put("appVersion", appVersion);
        HttpAsyncTask asyncTask = new HttpAsyncTask() {
            @Override
            public void onStart(String taskId) {
            }

            @Override
            public void onFinish(String taskId, String response) {
            }
        };
        if (!TextUtils.isEmpty(params.get("uid")) && !TextUtils.isEmpty(params.get("appVersion"))) {
            asyncTask.setDebug(true);
            asyncTask.excute(url, params, "POST");
        }

    }

    /**
     * 推送绑定
     */
    private void bindPush(String pushToken) {
        String url = Url.BaseUrl + Url.UserBindPush;
        Map<String, String> params = new HashMap<>();
        params.put("uid", userId);
        params.put("pushToken", pushToken);
        HttpAsyncTask asyncTask = new HttpAsyncTask() {
            @Override
            public void onStart(String taskId) {
            }

            @Override
            public void onFinish(String taskId, String response) {
            }
        };
        if (!TextUtils.isEmpty(params.get("uid")) && !TextUtils.isEmpty(pushToken)) {
            asyncTask.setDebug(true);
            asyncTask.excute(url, params, "POST");
        }
    }

}
